//
//  ScanResultViewController.m
//  LBXScanDemo
//
//  Created by lbxia on 15/11/17.
//  Copyright © 2015年 lbxia. All rights reserved.
//

#import "ScanResultViewController.h"
#import "PHAddUtils.h"

@interface ScanResultViewController ()<QMUITextFieldDelegate>
@property (strong, nonatomic) UIImageView *scanImg;
@property (strong, nonatomic) UILabel *labelScanText;
@property (strong, nonatomic) QMUITextField *nameTextField;
@property (nonatomic, strong) QMUIFillButton *inputBtn;
@end

@implementation ScanResultViewController

- (void)initSubviews {
    [super initSubviews];
    
    _scanImg = [[UIImageView alloc] initWithFrame:CGRectZero];
    _scanImg.backgroundColor = [UIColor clearColor];
    _scanImg.layer.borderWidth = PixelOne;
    _scanImg.layer.borderColor = GQDThemeManager.currentTheme.themeTintColor.CGColor;
    _scanImg.layer.masksToBounds = YES;
    [self.myView addSubview:_scanImg];
    
    _labelScanText = [[QMUILabel alloc] init];
    _labelScanText.font = CodeFontMake(14);
    _labelScanText.textColor = UIColorGray3;
    _labelScanText.textAlignment = NSTextAlignmentCenter;
    [self.myView addSubview:_labelScanText];
    
    _nameTextField = [[QMUITextField alloc] init];
    _nameTextField.borderStyle = UITextBorderStyleNone;
    _nameTextField.returnKeyType = UIReturnKeyDone;
    _nameTextField.backgroundColor = UIColorClear;
    _nameTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _nameTextField.font = CodeFontMake(14);
    _nameTextField.textColor = UIColorBlack;
    _nameTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _nameTextField.clearButtonMode = UITextFieldViewModeAlways;
    _nameTextField.placeholder = NSLocalizedString(@"tips_input_name", nil);
    _nameTextField.delegate = self;
    _nameTextField.qmui_borderPosition = QMUIBorderViewPositionBottom | QMUIBorderViewPositionTop;
    _nameTextField.textAlignment = NSTextAlignmentCenter;
    _nameTextField.enablesReturnKeyAutomatically = YES;
    [_nameTextField addTarget:self action:@selector(updateSubmitButtonEnables) forControlEvents:UIControlEventEditingChanged];
    [self.myView addSubview:_nameTextField];
    
    _inputBtn = [[QMUIFillButton alloc] initWithFillType:QMUIFillButtonColorRed];
    _inputBtn.titleLabel.font = CodeFontMake(16);
    [_inputBtn setTitle:NSLocalizedString(@"btn_add_sure", nil) forState:UIControlStateNormal];
    [_inputBtn addTarget:self action:@selector(okAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:_inputBtn];
}

- (void)updateSubmitButtonEnables {
    _inputBtn.enabled = _nameTextField.text.length > 0;
}

-(void)okAction:(id)sender {
    
    PHDevice *device = [[PHDevice alloc] init];
    device.device_type = GPHAddUtils.selectedIndex + 1;
    device.device_code = _strScan;
    device.device_nick_name = _nameTextField.text;
    
    WS(ws);
    [device bind:^(BOOL success) {
        if (success) {
            [GPHDeviceManager getBoundDevices:^(BOOL success) {
                
            }];
            
            [ws.navigationController popToRootViewControllerAnimated:YES];
        }
    }];
}

- (void)viewDidLayoutSubviews {
    self.shouldShowMyView = YES;
    [super viewDidLayoutSubviews];
    
    _scanImg.cornerRadius = 80.0f;
    _scanImg.frame = CGRectMake(self.myView.qmui_width / 2 - 80, 20, 160, 160);
    _labelScanText.frame = CGRectMake(30, _scanImg.qmui_bottom + 10, self.myView.qmui_width - 60, 20);
    _nameTextField.frame = CGRectMake(30, _labelScanText.qmui_bottom + 10, self.myView.qmui_width - 60, 40);
    _inputBtn.frame = CGRectMake(30, _nameTextField.qmui_bottom + 20, self.myView.qmui_width - 60, 40);
}

- (void)setupNavigationItems {
    [super setupNavigationItems];
    self.title = NSLocalizedString(@"title_scan_result", nil);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    _scanImg.image = [GPHAddUtils.selectedImage qmui_imageWithSpacingExtensionInsets:UIEdgeInsetsMake(20, 20, 20, 20)];
    _labelScanText.text = [NSString stringWithFormat:@"%@: %@",NSLocalizedString(@"dev_id", nil), _strScan];
    _nameTextField.text = GPHAddUtils.selectedName;
    
    [self updateSubmitButtonEnables];
}

#pragma mark - UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.view endEditing:YES];
    return YES;
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [self.view endEditing:YES];
}

@end
